//
//  TaskListViewController.m
//  PCR
//
//  Created by shumei on 16/6/3.
//  Copyright © 2016年 YM. All rights reserved.
//

#import "TaskListViewController.h"
#import "taskListCell.h"
#import "taskListModel.h"
#import "headView.h"
#import "sectionHeadView.h"

@interface TaskListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)UITableView *tableView;
@property(strong,nonatomic)NSMutableArray *dataArray;
@property(strong,nonatomic)NSMutableArray *dataArray2;

@end

@implementation TaskListViewController

- (NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSMutableArray *)dataArray2{
    if (_dataArray2 == nil) {
        _dataArray2 = [NSMutableArray array];
    }
    return _dataArray2;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTopTitleWithTitle:@"任务列表"];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height-114) style:UITableViewStyleGrouped];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"taskListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    
    
//    headView
    
    headView *headView =[[[NSBundle mainBundle] loadNibNamed:@"headView" owner:self options:nil] lastObject];
    headView.frame = CGRectMake(0, 0, MainSCreen.width, 110);
    _tableView.tableHeaderView = headView;
    
    for (NSInteger i = 0; i<4; i++) {
        taskListModel *model = [taskListModel new];
        [self.dataArray addObject:model];
    }
    for (NSInteger i = 0; i<4; i++) {
        taskListModel *model = [taskListModel new];
        [self.dataArray2 addObject:model];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - datasource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
       
        case 0:
            return _dataArray.count;
            break;
        default:
            return _dataArray2.count;
            break;
    }
    
    
    NSLog(@"test");
   
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    taskListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell =  [[taskListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        
        taskListModel *model = [self.dataArray objectAtIndex:indexPath.row];
        
        [cell updateBytaskListItem:model];
        
        [cell taskListSelectDesBtnBlock:^(BOOL isSelect) {
            [self.tableView reloadData];
        }];
    }else if (indexPath.section == 1 ){
        taskListModel *model = [self.dataArray2 objectAtIndex:indexPath.row];
        
        [cell updateBytaskListItem:model];
        
        [cell taskListSelectDesBtnBlock:^(BOOL isSelect) {
            [self.tableView reloadData];
        }];

    }
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        taskListModel *item = [self.dataArray objectAtIndex:indexPath.row];
        return item.itemHeight;
    }else if (indexPath.section == 1){
        taskListModel *item = [self.dataArray2 objectAtIndex:indexPath.row];
        return item.itemHeight;
    }else return 200;
    
  }

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    sectionHeadView *sectionHeadView =[[[NSBundle mainBundle] loadNibNamed:@"sectionHeadView" owner:self options:nil] lastObject];
    return sectionHeadView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 75;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
